package com.bjb.vr.remote.ws.rabbitmq.handler.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.rabbitmq.handler.MessageHandler;
import com.bjb.vr.remote.ws.websocket.entity.Action;
import com.bjb.vr.remote.ws.websocket.entity.Command;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import com.bjb.vr.remote.ws.websocket.websocket.WebSocketMsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @version 1.0.0
 * @description:设备批量下课
 * @author: caihaibo
 * @createDate: 2022/3/29
 */
@Slf4j
@Component
public class MultiClassOverVRMessageHandler implements MessageHandler {


    @Override
    public void process(RabbitWebSocketMsg rabbitWebSocketMsg) {

        JSONObject jsonObject = JSONObject.parseObject(rabbitWebSocketMsg.getMsg());

        Message message = JSONObject.toJavaObject(jsonObject, Message.class);

        if(message.getAction().equals(MessageActionConstants.MESSAGE_ACTION_VR_MULTI_CLASS_OVER)){
            log.info("处理多台设备下课信息");

            JSONObject _jsonObject = JSONObject.parseObject(message.getCommand().getParameter());
            JSONArray jsonArray = _jsonObject.getJSONArray("devUdids");

            if(jsonArray!=null  && jsonArray.size()>0){
                Action action = new Action();
                action.setAction(MessageActionConstants.MESSAGE_ACTION_STOP_CALSS);
                Command command = new Command<>();
                for(int i=0 ; i<jsonArray.size() ; i++){
                    String udid = (String) jsonArray.get(i);
                    //command.setUdid(udid);
                    action.setCommand(command);
                    rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
                    message.getCommand().setUdid(udid);
                    WebSocketMsgHandler.sendSingleClient(rabbitWebSocketMsg,message);
                }
                //设置为空消息，防止其他handler处理
                action.setAction(MessageActionConstants.MESSAGE_ACTION_EMPTY);
                rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
            }


        }

    }
}
